package cn.tedu.ivos.geofence.servie.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceParam;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.servie.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;
    @Qualifier("vehicleMapper")
    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        log.debug("开始处理查询电子围栏的业务，参数：{}", geofenceQuery);
        List<GeofenceVO> list = geofenceMapper.selectGeofence(geofenceQuery);
        return list;
    }

    @Override
    public void deleteGeofence(Long id) {
        log.debug("开始处理删除电子围栏的业务，参数：{}", id);
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        if(list !=null && list.size() > 0){
            throw new ServiceException(StatusCode.VEHICLE_EXISTS);
        }else {
            geofenceMapper.deleteGeofence(id);
        }
    }

    @Override
    public void updateStatus(Long id, String status) {
        log.debug("开始处理更新电子围栏的业务，参数：{}", id);
        geofenceMapper.updateStatus(id, status);
    }

    @Override
    public void saveGeofence(GeofenceParam geofenceParam) {
        log.debug("开始处理添加电子围栏的业务，参数：{}", geofenceParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceParam, geofence);
        geofence.setStatus("1");
        geofence.setCreateTime(new Date());


        geofenceMapper.insert(geofence);
    }
}


































